<?php
use think\facade\Cache;


/**
 * 对用户的密码进行加密
 * @param $password
 * @param $encrypt //传入加密串，在修改密码时做认证
 * @return array/password
 */
function encrypt_password($password)
{
    $pass = md5(md5($password));
    return $pass;
}

function Get_Ratio($price){
    //金额转积分
    $ratio = \app\common\model\Cost::get(1)['ratio'];
    $balance = $ratio * $price;
    return $balance;
}

function json_x($type = '-1', $msg = '失败')
{
    return json(['code' => $type, 'msg' => $msg]);
}

function json_ask($type = '-1', $msg = '失败', $answer = '0')
{
    return json(['code' => $type, 'msg' => $msg, 'answer' => $answer]);
}

function json_l($j, $count, $msg)
{
    $list["msg"] = $msg;
    $list["code"] = 0;
    $list["count"] = $count;
    $list["data"] = $j;
    return json($list);
}

function json_img($src)
{
    $img['code'] = 0;
    $img['msg'] = '成功';
    $img['src'] = $src;
    return json($img);
}

function seo($data = '', $type = 0)
{
    /**
     * $type = 0; //首页
     * $type = 1; //分类页
     * $type = 2; //二级分类页
     * $type = 3; //分类聚合页
     * $type = 4; //试题页
     * $type = 5; //其他页面 直接文字
     */
    $site_seo = array();
    $def = \app\common\model\SiteConfig::get(1);
    if ($type == 0) {
        //首页不进行任何处理
        $site_seo = [
            'site_title' => $def['site_title'],
            'site_description' => $def['site_description'],
            'site_keyword' => $def['site_keyword']
        ];
    }
    if ($type == 1) {
        $site_seo = [
            'site_title' => $data['name'] . '-' . $def['site_title'],
            'site_description' => $data['desc'],
            'site_keyword' => $data['key']
        ];
    }
    if ($type == 2) {
        $site_seo = [
            'site_title' => $data['two_name'] . '-' . $data['one_name'] . '-' . $def['site_suffix'],
            'site_description' => $data['desc'],
            'site_keyword' => $data['key']
        ];
    }
    if ($type == 3) {
        $site_seo = [
            'site_title' => '全部分类' . '-' . $def['site_title'],
            'site_description' => $def['site_description'],
            'site_keyword' => $def['site_keyword']
        ];
    }
    if ($type == 4) {
        //标题
        $len = 60 - strlen($def['site_suffix']);//去除尾巴剩余长度
        $question_title = substr($data['title'], 0, $len);

        if (strlen($data['option']) > 120) {
            $question_option = substr($data['option'], 0, 120);
        } else {
            //选项内容太少，选项 + 官网描述
            $question_option = $data['option'] . $def['site_description'];
        }
        $site_seo = [
            'site_title' => $question_title . ' ' . $def['site_suffix'],
            'site_description' => strip_tags($question_option),
            'site_keyword' => $def['site_keyword']
        ];
    }
    if ($type == 5) {
        $site_seo = [
            'site_title' => $data . '-' . $def['site_title'],
            'site_description' => $def['site_description'],
            'site_keyword' => $def['site_keyword']
        ];
    }
    return array_merge(Toarray($def), $site_seo);
}
function GetOrder(){
    $str = date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    return md5($str);
}

function Toarray(&$object)
{
    $object = json_decode(json_encode($object), true);
    return $object;
}

function p_json($data)
{
    $j['status'] = [
        'code' => 200,
        'message' => '成功'
    ];
    $j['data'] = $data;
    return json($j);
}

function delete_dir_file($dir_name)
{
    $result = false;
    if (is_dir($dir_name)) {
        if ($handle = opendir($dir_name)) {
            while (false !== ($item = readdir($handle))) {
                if ($item != '.' && $item != '..') {
                    if (is_dir($dir_name . DIRECTORY_SEPARATOR . $item)) {
                        delete_dir_file($dir_name . DIRECTORY_SEPARATOR . $item);
                    } else {
                        unlink($dir_name . DIRECTORY_SEPARATOR . $item);
                    }
                }
            }
            closedir($handle);
            if (rmdir($dir_name)) {
                $result = true;
            }
        }
    }
    return $result;
}

function CacheSet($name='',$value=''){
    Cache::set($name,$value,3600);
}
function CacheGet($name='',$value=''){
    return Cache::get($name,false);
}
function CacheClear(){
    Cache::clear();
}